home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
et
/
et3_0-a1.lha
/
et3
/
src
/
System.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-07
|
6KB
|
375 lines
#ifdef __GNUG__
#pragma implementation
#endif
#include "System.h"
#include "Class.h"
#include "CLib.h"
#include "String.h"
#include "Error.h"
#include "Directory.h"
#include "OrdColl.h"
#include "Env.h"
#include "Point.h"
char *gEtDir;
System *gSystem;
char *gProgname= "*** progname not set ***";
bool gInMain;
int gDebug;
bool gQuitApp;
static bool cleanuplock;
void ETInit(char **argv)
{
char *a0;
if (gSystem)
return;
if (argv && argv[0])
gProgname= argv[0];
gInMain= TRUE;
InitSystem();
gEtDir= CLib::Getenv("ET_DIR");
if (gEtDir == 0)
gEtDir= "/local/et";
for (int i= 0; a0= argv[i]; i++) {
if (a0[0] == '-' && a0[1] == 'E' && a0[2]) {
a0= &a0[2];
if (strcmp(a0, "d") == 0)
a0= "Debug";
Env::SetValue(a0, eEnvCommand);
gDebug= Env::GetValue("System.Debug", 0);
}
}
gDebug= Env::GetValue("System.Debug", 0);
if (Env::GetValue("System.MemStat", 0))
Storage::EnableStatistics();
int msize= Env::GetValue("System.MemStat.size", -1);
int mcnt= Env::GetValue("System.MemStat.cnt", -1);
if (msize != -1 || mcnt != -1)
Storage::EnableStatistics(msize, mcnt);
}
void ETCleanup()
{
SafeDelete(gSystem);
}
//---- SysEvtHandler -----------------------------------------------------------
NewAbstractMetaImpl(SysEvtHandler, Object, (T(resourceId), TP(owner)));
SysEvtHandler::SysEvtHandler(int id)
{
owner= 0;
resourceId= id;
}
SysEvtHandler::~SysEvtHandler()
{
if (!cleanuplock && owner) {
owner->ForEach(SeqCollection,RemovePtr)(this);
}
SafeDelete(owner);
}
void SysEvtHandler::Notify(SysEventCodes, int)
{
}
bool SysEvtHandler::HasInterest(SysEventCodes code)
{
return owner != 0;
}
void SysEvtHandler::AddOwner(SeqCollection *c)
{
if (owner == 0)
owner= new OrdCollection;
if (owner->FindPtr(c) == 0)
owner->Add(c);
}
bool SysEvtHandler::IsEqual(Object *op)
{
return resourceId == ((SysEvtHandler*)op)->resourceId;
}
void SysEvtHandler::Remove()
{
if (owner) {
owner->ForEach(SeqCollection,RemovePtr)(this);
SafeDelete(owner);
}
gSystem->AddCleanupObject(this);
}
int SysEvtHandler::Error(int code)
{
return 0;
}
//---- System ------------------------------------------------------------------
NewMetaImpl(System, Object, (TS(errorstr), TS(osid), TB(done), TP(fileInputHandler),
TP(fileOutputHandler), TP(zombieHandler), TP(signalHandler),
TP(asyncSignalHandler), TP(timeoutHandler), TP(cleanupList),
TS(gEtDir), TS(gProgname), TB(gInMain), TB(gDebug), TB(gQuitApp),
TSP(CLib::Environ)));
System::System(char *name)
{
osid= name;
}
bool System::Init()
{
fileInputHandler = new OrdCollection;
fileOutputHandler = new OrdCollection;
zombieHandler = new OrdCollection;
signalHandler = new OrdCollection;
asyncSignalHandler = new OrdCollection;
timeoutHandler = new OrdCollection;
workHandler = new OrdCollection;
cleanupList= 0;
return FALSE;
}
static void remove(SeqCollection *&oc)
{
if (oc) {
Iter next(oc);
register SysEvtHandler *seh;
while (seh= (SysEvtHandler*) next())
gSystem->AddCleanupObject(seh);
SafeDelete(oc);
}
}
System::~System()
{
remove(fileInputHandler);
remove(fileOutputHandler);
remove(zombieHandler);
remove(signalHandler);
remove(asyncSignalHandler);
remove(timeoutHandler);
remove(workHandler);
if (gSystem == this)
gSystem= 0;
/*
Remove();
*/
if (cleanupList) {
cleanuplock= TRUE;
cleanupList->FreeAll();
cleanuplock= FALSE;
SafeDelete(cleanupList);
}
}
void System::Control()
{
done= FALSE;
while (! done) {
InnerLoop(100);
if (cleanupList)
Remove();
}
}
void System::ExitControl()
{
gQuitApp= done= TRUE;
}
void System::InnerLoop(int)
{
AbstractMethod("InnerLoop");
}
Directory *System::MakeDirectory(char *name)
{
return new Directory(name);
}
int System::GetPathInfo(char*, int*, long*, int*, long*)
{
AbstractMethod("GetPathInfo");
return -1;
}
bool System::AccessPathName(char*, int)
{
return FALSE;
}
bool System::ExpandPathName(char*, int)
{
return FALSE;
}
bool System::ChangeDirectory(char *)
{
return FALSE;
}
char *System::WorkingDirectory()
{
return 0;
}
char *System::HomeDirectory()
{
return 0;
}
void System::Rename(char *from, char *to)
{
CLib::Unlink(to);
if (CLib::Link(from, to) != -1)
CLib::Unlink(from);
}
void System::Wait(unsigned int)
{
}
int System::GetPid()
{
return -1;
}
bool System::CanRead(int, int)
{
return FALSE;
}
bool System::CanWrite(int, int)
{
return FALSE;
}
void System::Remove()
{
while (cleanupList != 0) {
SeqCollection *tmp= cleanupList;
cleanupList= 0;
tmp->FreeAll();
delete tmp;
}
}
void System::AddHandler(SeqCollection *s, SysEvtHandler *h)
{
s->Add(h);
h->AddOwner(s);
}
SysEvtHandler *System::RemoveHandler(SeqCollection *s, SysEvtHandler *h)
{
return (SysEvtHandler*) s->RemovePtr(h);
}
void System::AddCleanupObject(Object *op)
{
if (cleanupList == 0)
cleanupList= new OrdCollection;
if (cleanupList->FindPtr(op) == 0)
cleanupList->Add(op);
}
void System::Abort(int)
{
CLib::Abort();
}
void System::Exit(int code, bool mode)
{
if (mode)
CLib::Exit(code);
CLib::FastExit(code);
}
PttyConnection* System::MakePttyConnection(char *, char **)
{
return 0;
}
//---- environment manipulation ------------------------------------------------
char *System::Getenv(char*)
{
AbstractMethod("Getenv");
return 0;
}
void System::Setenv(char*, char*)
{
AbstractMethod("Setenv");
}
void System::Unsetenv(char *name)
{
Setenv(name, 0);
}
Object *System::Load(char*, char*)
{
AbstractMethod("Load");
return 0;
}
char *System::GetError()
{
return form("errno: %d", CLib::ErrNo);
}
char *System::DirName(char *pathname)
{
if (strchr(pathname, '/')) {
static char buf[1000];
strcpy(buf, pathname);
char *r= strrchr(buf, '/');
if (r != buf)
*r= '\0';
return buf;
}
return WorkingDirectory();
}
//---- RPC ---------------------------------------------------------------------
int System::OpenConnection(char*, char*)
{
return -1;
}
int System::AnnounceTcpService(char*)
{
return -1;
}
int System::AnnounceUnixService(char*)
{
return -1;
}
int System::AcceptConnection(int)
{
return -1;
}